﻿using ConnectDatabase;
using Database;
using IBE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class Confirm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["BookingCode"]))
                    {
                        tblBooking booking = new BookingDB().GetByBookingCode(Request.QueryString["BookingCode"]);
                        if (booking != null)
                        {
                            //Booking code
                            lblBookingCode.Text = booking.BookingCode;

                            //Flight
                            
                            Flight departureFlight = GetDepartureFlight();
                            if (departureFlight != null)
                            {
                                repDepartureFlightDetail.DataSource = departureFlight.ListAvailFlt;
                                repDepartureFlightDetail.DataBind();

                                Flight returnFlight = GetReturnFlight();
                                if (returnFlight != null)
                                {
                                    repReturnFlightDetail.DataSource = returnFlight.ListAvailFlt;
                                    repReturnFlightDetail.DataBind();
                                    pnReturn.Visible = true;
                                }

                                FlightDetail.Visible = true;
                                FlightSummary.Visible = false;
                            }
                            else
                            {
                                tblAirline airline = new AirlinesDB().GetByKey(booking.AirlineCode);
                                tblGeoAirport startPoint = new GeoAirportDB().GetByKey(booking.DepartureAirportCode);
                                tblGeoAirport endPoint = new GeoAirportDB().GetByKey(booking.DestinationAirportCode);

                                if (airline != null)
                                {
                                    imgAirlineLogo.ImageUrl = airline.Logo != null ? "/Images/Airline/" + airline.Logo : "/Images/Airline/default.png";
                                }
                                else
                                {
                                    imgAirlineLogo.ImageUrl = "/Images/Airline/default.png";
                                }

                                lblStartPoint.Text = startPoint != null ? startPoint.tblGeoCity.Name_Vi : booking.DepartureAirportCode;
                                lblEndPoint.Text = endPoint != null ? endPoint.tblGeoCity.Name_Vi : booking.DestinationAirportCode;
                                lblStartTime.Text = booking.DepartureDate.Value.ToString("HH:mm");
                                lblStartDate.Text = booking.DepartureDate.Value.ToString("dd/MM/yyyy");
                                lblNumStop.Text = booking.TransitPoint == 0 ? "Chuyến bay thẳng" : booking.TransitPoint.ToString();

                                FlightDetail.Visible = false;
                                FlightSummary.Visible = true;
                            }
                            
                            //Passengers
                            List<tblPassenger> listPax = new PassengerDB().GetByBookingId(booking.Id);
                            repPax.DataSource = listPax;
                            repPax.DataBind();
                            lblGrandTotal.Text = (booking.TotalPrice.Value + booking.TotalFee.Value).ToString("#,0.##") + " " + booking.CurrencyCode;

                            //Contact
                            lblContactTitle.Text = booking.ContactTitle;
                            lblName.Text = booking.FullName;
                            lblPhone.Text = booking.Phone;
                            lblEmail.Text = booking.Email;
                            lblAddress.Text = booking.Address;
                            lblNote.Text = booking.Remark;
                        }
                        else
                        {
                            Response.Redirect("~/BookingFailed.aspx", false);
                        }
                    }
                }
                catch
                {
                    Response.Redirect("~/BookingFailed.aspx", false);
                }
            }
        }

        protected Flight GetDepartureFlight()
        {
            Flight flight = null;
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["BookingCode"]))
                {
                    if (Session["DepartureFlight" + Request.QueryString["BookingCode"]] != null)
                    {
                        flight = (Flight)Session["DepartureFlight" + Request.QueryString["BookingCode"]];
                    }
                }
            }
            catch
            {
                return null;
            }

            return flight;
        }

        protected Flight GetReturnFlight()
        {
            Flight flight = null;
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["BookingCode"]))
                {
                    if (Session["ReturnFlight" + Request.QueryString["BookingCode"]] != null)
                    {
                        flight = (Flight)Session["ReturnFlight" + Request.QueryString["BookingCode"]];
                    }
                }
            }
            catch
            {
                return null;
            }

            return flight;
        }
    }
}